import { Component, Input, OnInit } from '@angular/core';
import { MyModalHelper } from '@core/my-modal.helper';
import { YESNO_OPTION } from '../../../constant/dictionary';
import { myValidNull } from '@core/util/string-utils';
import { NzMessageService } from 'ng-zorro-antd';
import { OtcRuleLogService } from '@core/routes-services/otc-rule-log.service';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { OtcShopShopCreditRuleComponent } from '../shop-credit-rule/shop-credit-rule.component';

@Component({
  selector: 'app-otc-rule-log',
  templateUrl: './otc-rule-log.component.html',
})
export class OtcShopOtcRuleLogComponent implements OnInit {



  @Input() brandId: any;
  @Input() shopId: any;
  isLoading = false;
  keyword: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };
  yesOrNo: any [] = YESNO_OPTION;

  constructor(private message: NzMessageService,
              private router: Router,
              private route: ActivatedRoute,
              private otcRuleLogService: OtcRuleLogService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.route.queryParams.forEach((params: Params) => {
      let brandId =  +params['brandId'];
      let shopId =  +params['shopId'];
      if(!isNaN(brandId)) {
        this.brandId = brandId;
        this.shopId = shopId;
        this.initPage();
      }
    });
  }

  initPage() {
    this.isLoading = true;
    this.otcRuleLogService.logPagination(this.page, this.keyword, this.brandId, this.shopId).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  editEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(OtcShopShopCreditRuleComponent, { entity: bak ,showType: 2}, 1100, {
      nzTitle: '品牌信息',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

}
